//
//  CYChanPinXiangQingViewController.m
//  cyydt
//
//  Created by chu on 2019/1/29.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYChanPinXiangQingViewController.h"

@interface CYChanPinXiangQingViewController ()<WKNavigationDelegate, SDCycleScrollViewDelegate>

@property(nonatomic,strong)SDCycleScrollView *bannerView;

@property(nonatomic,assign)CGFloat webHeight;

@end

@implementation CYChanPinXiangQingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"产品详情";
    self.bannerViewHeightConstraint.constant = kWindowW * 248 / 375;
    [self.backView addSubview:self.bannerView];
    
    self.titleLabel.text = self.model.item_title;
    [self.contrainerView addSubview:self.webView];
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.webView.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame) + 30, kWindowW, self.webHeight);
    self.backViewHeightConstraint.constant = CGRectGetMaxY(self.webView.frame) + 15;
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    __weak typeof(self)weakself = self;
    [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable any, NSError * _Nullable error) {
        NSString *heightStr = [NSString stringWithFormat:@"%@",any];
        NSLog(@"heightStr1 --- %@",heightStr);
        weakself.webHeight = [heightStr floatValue] + 15;
        weakself.webView.frame = CGRectMake(0, CGRectGetMaxY(self.titleLabel.frame) + 30, kWindowW, self.webHeight);
        weakself.backViewHeightConstraint.constant = CGRectGetMaxY(self.webView.frame);
    }];
}

- (SDCycleScrollView *)bannerView{
    if (!_bannerView) {
        _bannerView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kWindowW, kWindowW * 248 / 375) delegate:self placeholderImage:[UIImage imageNamed:@""]];
        NSMutableArray *images = [NSMutableArray arrayWithCapacity:0];
        [images addObject:self.model.dpa_img_1];
        [images addObject:self.model.dpa_img_2];
        [images addObject:self.model.dpa_img_3];
        [images addObject:self.model.dpa_img_dan1];
        [images addObject:self.model.dpa_img_dan1];
        _bannerView.localizationImageNamesGroup = images;
    }
    return _bannerView;
}

- (WKWebView *)webView{
    if (!_webView) {
        NSString *jScript =@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta); var imgs = document.getElementsByTagName('img');for (var i in imgs){imgs[i].style.maxWidth='100%';imgs[i].style.height='auto';}";
        
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        
        wkWebConfig.userContentController = wkUController;
        
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 0) configuration:wkWebConfig];
        _webView.navigationDelegate = self;
        _webView.opaque = NO;
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.scrollView.scrollEnabled = NO;
        [_webView loadHTMLString:self.model.intro_sg baseURL:[NSURL URLWithString:HOST]];
    }
    return _webView;
}

@end
